package com.hst.capacity.domain.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import com.hst.capacity.domain.model.entity.order.specialcar.SpCarOrderEntity;
import com.hst.capacity.domain.model.request.specialcar.SpListParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.order.OrderVO;
import com.hst.capacity.domain.service.SpecialService;
import com.hst.capacity.infra.convertor.OrderPoolConvertor;
import com.hst.capacity.infra.convertor.SpecialConvertor;
import com.hst.capacity.infra.persistence.jpa.order.specialcar.SpCarOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SpecialServiceImpl implements SpecialService {

    @Autowired
    private SpCarOrderRepository orderRepository;

    /**
     * 分页并条件查询今日专车信息
     * @param param
     * @return
     */
    @Override
    public PagingResultVO<SpCarOrderEntity> searchWithConditionsAndPaging(SpListParam param) {
        Page<SpCarOrderEntity> page = orderRepository.findAll(new Specification<SpCarOrderEntity>() {
            @Override
            public Predicate toPredicate(Root<SpCarOrderEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                // 定义条件LIST
                List<Predicate> predicates = new ArrayList<Predicate>();
                //idFranchisee
                if (ObjectUtil.isNotEmpty(param.getIdFranchisee())) {
                    predicates.add(criteriaBuilder.equal(root.get("idFranchisee"),param.getIdFranchisee()));
                }
                //status
                if (StrUtil.isNotEmpty(param.getOrderStatus())) {
                    List<Integer> strings = Convert.toList(Integer.class, param.getOrderStatus());
                    CriteriaBuilder.In<Integer> in = criteriaBuilder.in(root.get("status"));
                    for (Integer v:strings) {
                        in.value(v);
                    }
                    predicates.add(criteriaBuilder.and(in));
                }
                //orderSource
                if (StrUtil.isNotEmpty(param.getOrderSources())) {
                    List<Integer> strings = Convert.toList(Integer.class, param.getOrderSources());
                    CriteriaBuilder.In<Integer> in = criteriaBuilder.in(root.get("orderSource"));
                    for (Integer v:strings) {
                        in.value(v);
                    }
                    predicates.add(criteriaBuilder.and(in));
                }
                //paymentStatus
                if (StrUtil.isNotEmpty(param.getPayStatus())) {
                    List<Integer> strings = Convert.toList(Integer.class, param.getPayStatus());
                    CriteriaBuilder.In<Integer> in = criteriaBuilder.in(root.get("paymentStatus"));
                    for (Integer v:strings) {
                        in.value(v);
                    }
                    predicates.add(criteriaBuilder.and(in));
                }
//                //regionCode 城际原表中没有regionCode
//                if (StrUtil.isNotEmpty(param.getRegionCodes())) {
//                    List<Integer> strings = Convert.toList(Integer.class, param.getRegionCodes());
//                    CriteriaBuilder.In<Integer> in = criteriaBuilder.in(root.get("regionCode"));
//                    for (Integer v:strings) {
//                        in.value(v);
//                    }
//                    predicates.add(criteriaBuilder.and(in));
//                }


                //排序
                query.orderBy(criteriaBuilder.desc(root.get("createTime")));
                Predicate[] array = predicates.toArray(new Predicate[0]);
                return criteriaBuilder.and(array);
            }
        }, PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
        return new PagingResultVO(page.getTotalElements(),page.getContent());
    }

    @Override
    public OrderVO findOrderByIdOrder(String idOrder) {
//        SpCarOrderEntity order = orderRepository.getById(idOrder);
//        if (order == null)
//            throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
//
//        return SpecialConvertor.INSTANCE.convert()
        return null;
    }

}
